{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "69b45e82",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 店铺流量\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 3)\n",
    "end_date = datetime(2023, 12, 3)\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 构建链接\n",
    "    url = f\"https://sycm.taobao.com/flow/gray/excel.do?spm=a21ag.11910098.sycm-flow-shop-source-construction-flow-source-self-table-new.1.9afe50a5VcaARz&_path_=v4/excel/shop/source/v3&device=2&dateType=day&dateRange={formatted_date}|{formatted_date}&belong=all\"\n",
    "\n",
    "    # 在默认浏览器中打开链接\n",
    "    webbrowser.open(url)\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6039fa2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 全量商品\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 1)\n",
    "end_date = datetime(2023, 12, 3)\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 构建链接\n",
    "    url = f\"https://sycm.taobao.com/cc/cockpit/marcro/item/excel/top.json?spm=a21ag.12100459.0.0.284550a5fl0ZSd&dateRange={formatted_date}|{formatted_date}&dateType=day&pageSize=10&page=1&order=desc&orderBy=payAmt&dtUpdateTime=false&keyword=&follow=false&cateId=&cateLevel=&guideCateId=&device=0&indexCode=itmUv%2CitemCartCnt%2CpayItmCnt%2CpayAmt%2CpayRate\"\n",
    "\n",
    "    # 在默认浏览器中打开链接\n",
    "    webbrowser.open(url)\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ec8fd5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单品流量\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 22)\n",
    "end_date = datetime(2023, 12, 24)\n",
    "item_id_variable = \"719355771778\"\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 构建链接\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id_variable}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在默认浏览器中打开链接\n",
    "    webbrowser.open(url)\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7268c268",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 单品流量\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# 定义日期变量和商品ID变量\n",
    "date_variable = datetime(2023, 12, 22)\n",
    "item_id_variable = \"719355771778\"\n",
    "\n",
    "# 格式化日期\n",
    "formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "# 构建链接\n",
    "url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id_variable}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "# 在默认浏览器中打开链接\n",
    "webbrowser.open(url)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5d8179f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# 定义日期变量\n",
    "date_variable = datetime(2023, 12, 5)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量'\n",
    "\n",
    "# 获取父文件夹下的所有子文件夹\n",
    "subfolders = [f.path for f in os.scandir(parent_folder) if f.is_dir()]\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in subfolders:\n",
    "    # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "    item_id = os.path.basename(subfolder)\n",
    "\n",
    "    # 构建链接\n",
    "    formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在默认浏览器中打开链接\n",
    "    webbrowser.open(url)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad5a4f9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "import os\n",
    "from datetime import datetime, timedelta\n",
    "import time\n",
    "\n",
    "# 定义日期变量\n",
    "date_variable = datetime(2023, 4, 5)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量'\n",
    "\n",
    "# 获取父文件夹下的所有子文件夹\n",
    "subfolders = [f.path for f in os.scandir(parent_folder) if f.is_dir()]\n",
    "\n",
    "# 设置Chrome浏览器的下载路径\n",
    "download_folder = 'D:\\新建文件夹\\新建文件夹'\n",
    "\n",
    "# 设置Chrome浏览器的配置\n",
    "chrome_options = Options()\n",
    "chrome_options.add_experimental_option(\"prefs\", {\"download.default_directory\": download_folder})\n",
    "\n",
    "# 启动Chrome浏览器\n",
    "driver = webdriver.Chrome(options=chrome_options)\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in subfolders:\n",
    "    # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "    item_id = os.path.basename(subfolder)\n",
    "\n",
    "    # 构建链接\n",
    "    formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在浏览器中打开链接\n",
    "    driver.get(url)\n",
    "\n",
    "    # 等待一段时间以确保下载完成\n",
    "    time.sleep(5)\n",
    "\n",
    "    # 移动下载好的表格到相应的文件夹\n",
    "    downloaded_file = max([os.path.join(download_folder, f) for f in os.listdir(download_folder)], key=os.path.getctime)\n",
    "    destination_path = os.path.join(subfolder, f\"{item_id}_{formatted_date}_data.xlsx\")\n",
    "    os.rename(downloaded_file, destination_path)\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "71dc00db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Collecting msedge-selenium-tools\n",
      "  Downloading msedge_selenium_tools-3.141.4-py3-none-any.whl (15 kB)\n",
      "Collecting selenium==3.141 (from msedge-selenium-tools)\n",
      "  Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)\n",
      "     ---------------------------------------- 0.0/904.6 kB ? eta -:--:--\n",
      "     ---------------------------------------- 10.2/904.6 kB ? eta -:--:--\n",
      "     --- ----------------------------------- 81.9/904.6 kB 1.5 MB/s eta 0:00:01\n",
      "     -------- ----------------------------- 204.8/904.6 kB 2.1 MB/s eta 0:00:01\n",
      "     ----------------------- -------------- 553.0/904.6 kB 3.5 MB/s eta 0:00:01\n",
      "     ------------------------------------ - 880.6/904.6 kB 4.6 MB/s eta 0:00:01\n",
      "     -------------------------------------- 904.6/904.6 kB 4.1 MB/s eta 0:00:00\n",
      "Requirement already satisfied: urllib3 in d:\\anaconda\\lib\\site-packages (from selenium==3.141->msedge-selenium-tools) (1.26.16)\n",
      "Installing collected packages: selenium, msedge-selenium-tools\n",
      "  Attempting uninstall: selenium\n",
      "    Found existing installation: selenium 4.16.0\n",
      "    Uninstalling selenium-4.16.0:\n",
      "      Successfully uninstalled selenium-4.16.0\n",
      "Successfully installed msedge-selenium-tools-3.141.4 selenium-3.141.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install msedge-selenium-tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b657a1a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\pc\\AppData\\Local\\Temp\\ipykernel_1840\\734114382.py:23: DeprecationWarning: Selenium Tools for Microsoft Edge is deprecated. Please upgrade to Selenium 4 which has built-in support for Microsoft Edge (Chromium): https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/#upgrading-from-selenium-3\n",
      "  driver = Edge(options=edge_options)\n"
     ]
    },
    {
     "ename": "WebDriverException",
     "evalue": "Message: 'MicrosoftWebDriver.exe' executable needs to be in PATH. Please download from http://go.microsoft.com/fwlink/?LinkId=619687\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\selenium\\webdriver\\common\\service.py:72\u001b[0m, in \u001b[0;36mService.start\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     71\u001b[0m     cmd\u001b[38;5;241m.\u001b[39mextend(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcommand_line_args())\n\u001b[1;32m---> 72\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprocess \u001b[38;5;241m=\u001b[39m \u001b[43msubprocess\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mPopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcmd\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43menv\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43menv\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     73\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mclose_fds\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mplatform\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msystem\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m!=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mWindows\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m     74\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mstdout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlog_file\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     75\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mstderr\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlog_file\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m     76\u001b[0m \u001b[43m                                    \u001b[49m\u001b[43mstdin\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mPIPE\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     77\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n",
      "File \u001b[1;32mD:\\Anaconda\\Lib\\subprocess.py:1026\u001b[0m, in \u001b[0;36mPopen.__init__\u001b[1;34m(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask, pipesize, process_group)\u001b[0m\n\u001b[0;32m   1023\u001b[0m             \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstderr \u001b[38;5;241m=\u001b[39m io\u001b[38;5;241m.\u001b[39mTextIOWrapper(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstderr,\n\u001b[0;32m   1024\u001b[0m                     encoding\u001b[38;5;241m=\u001b[39mencoding, errors\u001b[38;5;241m=\u001b[39merrors)\n\u001b[1;32m-> 1026\u001b[0m     \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_child\u001b[49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mexecutable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpreexec_fn\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mclose_fds\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1027\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mpass_fds\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcwd\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43menv\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1028\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mstartupinfo\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcreationflags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mshell\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1029\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mp2cread\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mp2cwrite\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1030\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mc2pread\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mc2pwrite\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1031\u001b[0m \u001b[43m                        \u001b[49m\u001b[43merrread\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43merrwrite\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1032\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mrestore_signals\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1033\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mgid\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgids\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43muid\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mumask\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1034\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mstart_new_session\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprocess_group\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1035\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m:\n\u001b[0;32m   1036\u001b[0m     \u001b[38;5;66;03m# Cleanup if the child failed starting.\u001b[39;00m\n",
      "File \u001b[1;32mD:\\Anaconda\\Lib\\subprocess.py:1538\u001b[0m, in \u001b[0;36mPopen._execute_child\u001b[1;34m(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_gid, unused_gids, unused_uid, unused_umask, unused_start_new_session, unused_process_group)\u001b[0m\n\u001b[0;32m   1537\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 1538\u001b[0m     hp, ht, pid, tid \u001b[38;5;241m=\u001b[39m \u001b[43m_winapi\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mCreateProcess\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexecutable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1539\u001b[0m \u001b[43m                             \u001b[49m\u001b[38;5;66;43;03m# no special security\u001b[39;49;00m\n\u001b[0;32m   1540\u001b[0m \u001b[43m                             \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m   1541\u001b[0m \u001b[43m                             \u001b[49m\u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mclose_fds\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1542\u001b[0m \u001b[43m                             \u001b[49m\u001b[43mcreationflags\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1543\u001b[0m \u001b[43m                             \u001b[49m\u001b[43menv\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1544\u001b[0m \u001b[43m                             \u001b[49m\u001b[43mcwd\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1545\u001b[0m \u001b[43m                             \u001b[49m\u001b[43mstartupinfo\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1546\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m   1547\u001b[0m     \u001b[38;5;66;03m# Child is launched. Close the parent's copy of those pipe\u001b[39;00m\n\u001b[0;32m   1548\u001b[0m     \u001b[38;5;66;03m# handles that only the child should have open.  You need\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   1551\u001b[0m     \u001b[38;5;66;03m# pipe will not close when the child process exits and the\u001b[39;00m\n\u001b[0;32m   1552\u001b[0m     \u001b[38;5;66;03m# ReadFile will hang.\u001b[39;00m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [WinError 2] 系统找不到指定的文件。",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mWebDriverException\u001b[0m                        Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 23\u001b[0m\n\u001b[0;32m     20\u001b[0m edge_options\u001b[38;5;241m.\u001b[39madd_argument(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mprefs: \u001b[39m\u001b[38;5;124m{\u001b[39m\u001b[38;5;124mdownload.default_directory: \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m download_folder \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m}\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     22\u001b[0m \u001b[38;5;66;03m# 启动Microsoft Edge浏览器\u001b[39;00m\n\u001b[1;32m---> 23\u001b[0m driver \u001b[38;5;241m=\u001b[39m \u001b[43mEdge\u001b[49m\u001b[43m(\u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43medge_options\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     25\u001b[0m \u001b[38;5;66;03m# 循环遍历每个子文件夹\u001b[39;00m\n\u001b[0;32m     26\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m subfolder \u001b[38;5;129;01min\u001b[39;00m subfolders:\n\u001b[0;32m     27\u001b[0m     \u001b[38;5;66;03m# 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\u001b[39;00m\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\msedge\\selenium_tools\\webdriver.py:104\u001b[0m, in \u001b[0;36mWebDriver.__init__\u001b[1;34m(self, executable_path, capabilities, port, verbose, service_log_path, log_path, keep_alive, desired_capabilities, service_args, options)\u001b[0m\n\u001b[0;32m     96\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mport \u001b[38;5;241m=\u001b[39m port\n\u001b[0;32m     98\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice \u001b[38;5;241m=\u001b[39m Service(\n\u001b[0;32m     99\u001b[0m         executable_path,\n\u001b[0;32m    100\u001b[0m         port\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mport,\n\u001b[0;32m    101\u001b[0m         verbose\u001b[38;5;241m=\u001b[39mverbose,\n\u001b[0;32m    102\u001b[0m         service_args\u001b[38;5;241m=\u001b[39mservice_args,\n\u001b[0;32m    103\u001b[0m         log_path\u001b[38;5;241m=\u001b[39mservice_log_path)\n\u001b[1;32m--> 104\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mservice\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstart\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    106\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m    107\u001b[0m     RemoteWebDriver\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__init__\u001b[39m(\n\u001b[0;32m    108\u001b[0m         \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m    109\u001b[0m         command_executor \u001b[38;5;241m=\u001b[39m EdgeRemoteConnection(\n\u001b[0;32m    110\u001b[0m         remote_server_addr\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice\u001b[38;5;241m.\u001b[39mservice_url,\n\u001b[0;32m    111\u001b[0m         keep_alive\u001b[38;5;241m=\u001b[39mkeep_alive),\n\u001b[0;32m    112\u001b[0m         desired_capabilities\u001b[38;5;241m=\u001b[39mdesired_capabilities)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\selenium\\webdriver\\common\\service.py:81\u001b[0m, in \u001b[0;36mService.start\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     79\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m     80\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m err\u001b[38;5;241m.\u001b[39merrno \u001b[38;5;241m==\u001b[39m errno\u001b[38;5;241m.\u001b[39mENOENT:\n\u001b[1;32m---> 81\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m WebDriverException(\n\u001b[0;32m     82\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m executable needs to be in PATH. \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (\n\u001b[0;32m     83\u001b[0m                 os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mbasename(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpath), \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstart_error_message)\n\u001b[0;32m     84\u001b[0m         )\n\u001b[0;32m     85\u001b[0m     \u001b[38;5;28;01melif\u001b[39;00m err\u001b[38;5;241m.\u001b[39merrno \u001b[38;5;241m==\u001b[39m errno\u001b[38;5;241m.\u001b[39mEACCES:\n\u001b[0;32m     86\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m WebDriverException(\n\u001b[0;32m     87\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m executable may have wrong permissions. \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (\n\u001b[0;32m     88\u001b[0m                 os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mbasename(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpath), \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstart_error_message)\n\u001b[0;32m     89\u001b[0m         )\n",
      "\u001b[1;31mWebDriverException\u001b[0m: Message: 'MicrosoftWebDriver.exe' executable needs to be in PATH. Please download from http://go.microsoft.com/fwlink/?LinkId=619687\n"
     ]
    }
   ],
   "source": [
    "from msedge.selenium_tools import Edge, EdgeOptions\n",
    "import os\n",
    "from datetime import datetime, timedelta\n",
    "import time\n",
    "\n",
    "# 定义日期变量\n",
    "date_variable = datetime(2023, 12, 5)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量'\n",
    "\n",
    "# 获取父文件夹下的所有子文件夹\n",
    "subfolders = [f.path for f in os.scandir(parent_folder) if f.is_dir()]\n",
    "\n",
    "# 设置Microsoft Edge浏览器的下载路径\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 设置Microsoft Edge浏览器的配置\n",
    "edge_options = EdgeOptions()\n",
    "edge_options.add_argument(\"prefs: {download.default_directory: '\" + download_folder + \"'}\")\n",
    "\n",
    "# 启动Microsoft Edge浏览器\n",
    "driver = Edge(options=edge_options)\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in subfolders:\n",
    "    # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "    item_id = os.path.basename(subfolder)\n",
    "\n",
    "    # 构建链接\n",
    "    formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在浏览器中打开链接\n",
    "    driver.get(url)\n",
    "\n",
    "    # 等待一段时间以确保下载完成\n",
    "    time.sleep(5)\n",
    "\n",
    "    # 移动下载好的表格到相应的文件夹\n",
    "    downloaded_file = max([os.path.join(download_folder, f) for f in os.listdir(download_folder)], key=os.path.getctime)\n",
    "    destination_path = os.path.join(subfolder, f\"{item_id}_{formatted_date}_data.xlsx\")\n",
    "    os.rename(downloaded_file, destination_path)\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b03296fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: stack-data in d:\\anaconda\\lib\\site-packages (0.2.0)\n",
      "Collecting stack-data\n",
      "  Obtaining dependency information for stack-data from https://files.pythonhosted.org/packages/f1/7b/ce1eafaf1a76852e2ec9b22edecf1daa58175c090266e9f6c64afcd81d91/stack_data-0.6.3-py3-none-any.whl.metadata\n",
      "  Downloading stack_data-0.6.3-py3-none-any.whl.metadata (18 kB)\n",
      "Collecting executing>=1.2.0 (from stack-data)\n",
      "  Obtaining dependency information for executing>=1.2.0 from https://files.pythonhosted.org/packages/80/03/6ea8b1b2a5ab40a7a60dc464d3daa7aa546e0a74d74a9f8ff551ea7905db/executing-2.0.1-py2.py3-none-any.whl.metadata\n",
      "  Downloading executing-2.0.1-py2.py3-none-any.whl.metadata (9.0 kB)\n",
      "Collecting asttokens>=2.1.0 (from stack-data)\n",
      "  Obtaining dependency information for asttokens>=2.1.0 from https://files.pythonhosted.org/packages/45/86/4736ac618d82a20d87d2f92ae19441ebc7ac9e7a581d7e58bbe79233b24a/asttokens-2.4.1-py2.py3-none-any.whl.metadata\n",
      "  Downloading asttokens-2.4.1-py2.py3-none-any.whl.metadata (5.2 kB)\n",
      "Requirement already satisfied: pure-eval in d:\\anaconda\\lib\\site-packages (from stack-data) (0.2.2)\n",
      "Requirement already satisfied: six>=1.12.0 in d:\\anaconda\\lib\\site-packages (from asttokens>=2.1.0->stack-data) (1.16.0)\n",
      "Downloading stack_data-0.6.3-py3-none-any.whl (24 kB)\n",
      "Downloading asttokens-2.4.1-py2.py3-none-any.whl (27 kB)\n",
      "Downloading executing-2.0.1-py2.py3-none-any.whl (24 kB)\n",
      "Installing collected packages: executing, asttokens, stack-data\n",
      "Successfully installed asttokens-2.4.1 executing-2.0.1 stack-data-0.6.3\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install --upgrade stack-data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "52cd29b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"PATH\"] += os.pathsep + r\"D:\\小工具\\edgedriver\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b3ae7c04",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单品流量\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 22)\n",
    "end_date = datetime(2023, 12, 24)\n",
    "item_id_variable = \"719355771778\"\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 构建链接\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id_variable}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在默认浏览器中打开链接\n",
    "    webbrowser.open(url)\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "91354fff",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\pc\\AppData\\Local\\Temp\\ipykernel_3500\\1008600283.py:26: DeprecationWarning: Selenium Tools for Microsoft Edge is deprecated. Please upgrade to Selenium 4 which has built-in support for Microsoft Edge (Chromium): https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/#upgrading-from-selenium-3\n",
      "  driver = Edge(executable_path=webdriver_path, options=edge_options)\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "expected str, bytes or os.PathLike object, not NoneType",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 26\u001b[0m\n\u001b[0;32m     23\u001b[0m webdriver_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mD:\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124m小工具\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124medgedriver\u001b[39m\u001b[38;5;124m\\\u001b[39m\u001b[38;5;124mmsedgedriver.exe\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m     25\u001b[0m \u001b[38;5;66;03m# 启动Microsoft Edge浏览器\u001b[39;00m\n\u001b[1;32m---> 26\u001b[0m driver \u001b[38;5;241m=\u001b[39m \u001b[43mEdge\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexecutable_path\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwebdriver_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43medge_options\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     28\u001b[0m \u001b[38;5;66;03m# 循环遍历每个子文件夹\u001b[39;00m\n\u001b[0;32m     29\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m subfolder \u001b[38;5;129;01min\u001b[39;00m subfolders:\n\u001b[0;32m     30\u001b[0m     \u001b[38;5;66;03m# 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\u001b[39;00m\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\msedge\\selenium_tools\\webdriver.py:104\u001b[0m, in \u001b[0;36mWebDriver.__init__\u001b[1;34m(self, executable_path, capabilities, port, verbose, service_log_path, log_path, keep_alive, desired_capabilities, service_args, options)\u001b[0m\n\u001b[0;32m     96\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mport \u001b[38;5;241m=\u001b[39m port\n\u001b[0;32m     98\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice \u001b[38;5;241m=\u001b[39m Service(\n\u001b[0;32m     99\u001b[0m         executable_path,\n\u001b[0;32m    100\u001b[0m         port\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mport,\n\u001b[0;32m    101\u001b[0m         verbose\u001b[38;5;241m=\u001b[39mverbose,\n\u001b[0;32m    102\u001b[0m         service_args\u001b[38;5;241m=\u001b[39mservice_args,\n\u001b[0;32m    103\u001b[0m         log_path\u001b[38;5;241m=\u001b[39mservice_log_path)\n\u001b[1;32m--> 104\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mservice\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstart\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    106\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m    107\u001b[0m     RemoteWebDriver\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__init__\u001b[39m(\n\u001b[0;32m    108\u001b[0m         \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m    109\u001b[0m         command_executor \u001b[38;5;241m=\u001b[39m EdgeRemoteConnection(\n\u001b[0;32m    110\u001b[0m         remote_server_addr\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mservice\u001b[38;5;241m.\u001b[39mservice_url,\n\u001b[0;32m    111\u001b[0m         keep_alive\u001b[38;5;241m=\u001b[39mkeep_alive),\n\u001b[0;32m    112\u001b[0m         desired_capabilities\u001b[38;5;241m=\u001b[39mdesired_capabilities)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\selenium\\webdriver\\common\\service.py:98\u001b[0m, in \u001b[0;36mService.start\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     91\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mstart\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m     92\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Starts the Service.\u001b[39;00m\n\u001b[0;32m     93\u001b[0m \n\u001b[0;32m     94\u001b[0m \u001b[38;5;124;03m    :Exceptions:\u001b[39;00m\n\u001b[0;32m     95\u001b[0m \u001b[38;5;124;03m     - WebDriverException : Raised either when it can't start the service\u001b[39;00m\n\u001b[0;32m     96\u001b[0m \u001b[38;5;124;03m       or when it can't connect to the service\u001b[39;00m\n\u001b[0;32m     97\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m---> 98\u001b[0m     \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_start_process\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    100\u001b[0m     count \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m    101\u001b[0m     \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\selenium\\webdriver\\common\\service.py:208\u001b[0m, in \u001b[0;36mService._start_process\u001b[1;34m(self, path)\u001b[0m\n\u001b[0;32m    205\u001b[0m         start_info\u001b[38;5;241m.\u001b[39mdwFlags \u001b[38;5;241m=\u001b[39m subprocess\u001b[38;5;241m.\u001b[39mCREATE_NEW_CONSOLE \u001b[38;5;241m|\u001b[39m subprocess\u001b[38;5;241m.\u001b[39mSTARTF_USESHOWWINDOW\n\u001b[0;32m    206\u001b[0m         start_info\u001b[38;5;241m.\u001b[39mwShowWindow \u001b[38;5;241m=\u001b[39m subprocess\u001b[38;5;241m.\u001b[39mSW_HIDE\n\u001b[1;32m--> 208\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprocess \u001b[38;5;241m=\u001b[39m \u001b[43msubprocess\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mPopen\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    209\u001b[0m \u001b[43m        \u001b[49m\u001b[43mcmd\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    210\u001b[0m \u001b[43m        \u001b[49m\u001b[43menv\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43menv\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    211\u001b[0m \u001b[43m        \u001b[49m\u001b[43mclose_fds\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mclose_file_descriptors\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    212\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstdout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlog_output\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    213\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstderr\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlog_output\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    214\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstdin\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mPIPE\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    215\u001b[0m \u001b[43m        \u001b[49m\u001b[43mcreationflags\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreation_flags\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    216\u001b[0m \u001b[43m        \u001b[49m\u001b[43mstartupinfo\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstart_info\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    217\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpopen_kw\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    218\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    219\u001b[0m     logger\u001b[38;5;241m.\u001b[39mdebug(\n\u001b[0;32m    220\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mStarted executable: `\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m` in a child process with pid: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m using \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m to output \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m    221\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_path,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    224\u001b[0m         \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog_output,\n\u001b[0;32m    225\u001b[0m     )\n\u001b[0;32m    226\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n",
      "File \u001b[1;32mD:\\Anaconda\\Lib\\subprocess.py:1026\u001b[0m, in \u001b[0;36mPopen.__init__\u001b[1;34m(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask, pipesize, process_group)\u001b[0m\n\u001b[0;32m   1022\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtext_mode:\n\u001b[0;32m   1023\u001b[0m             \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstderr \u001b[38;5;241m=\u001b[39m io\u001b[38;5;241m.\u001b[39mTextIOWrapper(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstderr,\n\u001b[0;32m   1024\u001b[0m                     encoding\u001b[38;5;241m=\u001b[39mencoding, errors\u001b[38;5;241m=\u001b[39merrors)\n\u001b[1;32m-> 1026\u001b[0m     \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_child\u001b[49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mexecutable\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpreexec_fn\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mclose_fds\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1027\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mpass_fds\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcwd\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43menv\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1028\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mstartupinfo\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcreationflags\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mshell\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1029\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mp2cread\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mp2cwrite\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1030\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mc2pread\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mc2pwrite\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1031\u001b[0m \u001b[43m                        \u001b[49m\u001b[43merrread\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43merrwrite\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1032\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mrestore_signals\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1033\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mgid\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgids\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43muid\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mumask\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1034\u001b[0m \u001b[43m                        \u001b[49m\u001b[43mstart_new_session\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprocess_group\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1035\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m:\n\u001b[0;32m   1036\u001b[0m     \u001b[38;5;66;03m# Cleanup if the child failed starting.\u001b[39;00m\n\u001b[0;32m   1037\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mfilter\u001b[39m(\u001b[38;5;28;01mNone\u001b[39;00m, (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstdin, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstdout, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstderr)):\n",
      "File \u001b[1;32mD:\\Anaconda\\Lib\\subprocess.py:1462\u001b[0m, in \u001b[0;36mPopen._execute_child\u001b[1;34m(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_gid, unused_gids, unused_uid, unused_umask, unused_start_new_session, unused_process_group)\u001b[0m\n\u001b[0;32m   1460\u001b[0m     args \u001b[38;5;241m=\u001b[39m list2cmdline([args])\n\u001b[0;32m   1461\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m-> 1462\u001b[0m     args \u001b[38;5;241m=\u001b[39m \u001b[43mlist2cmdline\u001b[49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   1464\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m executable \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m   1465\u001b[0m     executable \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mfsdecode(executable)\n",
      "File \u001b[1;32mD:\\Anaconda\\Lib\\subprocess.py:608\u001b[0m, in \u001b[0;36mlist2cmdline\u001b[1;34m(seq)\u001b[0m\n\u001b[0;32m    606\u001b[0m result \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m    607\u001b[0m needquote \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[1;32m--> 608\u001b[0m \u001b[43m\u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43marg\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mmap\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfsdecode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mseq\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[0;32m    609\u001b[0m \u001b[43m    \u001b[49m\u001b[43mbs_buf\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m    611\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;66;43;03m# Add a space to separate this argument from the others\u001b[39;49;00m\n",
      "File \u001b[1;32m<frozen os>:824\u001b[0m, in \u001b[0;36mfsdecode\u001b[1;34m(filename)\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: expected str, bytes or os.PathLike object, not NoneType"
     ]
    }
   ],
   "source": [
    "from msedge.selenium_tools import Edge, EdgeOptions\n",
    "import os\n",
    "from datetime import datetime, timedelta\n",
    "import time\n",
    "\n",
    "# 定义日期变量\n",
    "date_variable = datetime(2023, 12, 5)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "# 获取父文件夹下的所有子文件夹\n",
    "subfolders = [f.path for f in os.scandir(parent_folder) if f.is_dir()]\n",
    "\n",
    "# 设置Microsoft Edge浏览器的下载路径\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 设置Microsoft Edge浏览器的配置\n",
    "edge_options = EdgeOptions()\n",
    "edge_options.add_argument(\"prefs: {download.default_directory: '\" + download_folder + \"'}\")\n",
    "\n",
    "# 指定msedgedriver.exe的路径\n",
    "webdriver_path = 'D:\\小工具\\edgedriver\\msedgedriver.exe'\n",
    "\n",
    "# 启动Microsoft Edge浏览器\n",
    "driver = Edge(executable_path=webdriver_path, options=edge_options)\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in subfolders:\n",
    "    # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "    item_id = os.path.basename(subfolder)\n",
    "\n",
    "    # 构建链接\n",
    "    formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在浏览器中打开链接\n",
    "    driver.get(url)\n",
    "\n",
    "    # 等待一段时间以确保下载完成\n",
    "    time.sleep(5)\n",
    "\n",
    "    # 移动下载好的表格到相应的文件夹\n",
    "    downloaded_file = max([os.path.join(download_folder, f) for f in os.listdir(download_folder)], key=os.path.getctime)\n",
    "    destination_path = os.path.join(subfolder, f\"{item_id}_{formatted_date}_data.xlsx\")\n",
    "    os.rename(downloaded_file, destination_path)\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "74df91f4",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'str' object has no attribute 'get'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 39\u001b[0m\n\u001b[0;32m     36\u001b[0m url \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttps://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mformatted_date\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m|\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mformatted_date\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m&crowdType=all&device=2&itemId=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mitem_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m&device=2&order=desc&orderBy=uv\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     38\u001b[0m \u001b[38;5;66;03m# 在浏览器中打开链接\u001b[39;00m\n\u001b[1;32m---> 39\u001b[0m \u001b[43mdriver\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m(url)\n\u001b[0;32m     41\u001b[0m \u001b[38;5;66;03m# 等待一段时间以确保下载完成\u001b[39;00m\n\u001b[0;32m     42\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m5\u001b[39m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'str' object has no attribute 'get'"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "from selenium.webdriver.common.desired_capabilities import DesiredCapabilities\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "import os\n",
    "from datetime import datetime, timedelta\n",
    "import time\n",
    "\n",
    "# 定义日期变量\n",
    "date_variable = datetime(2023, 12, 5)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "# 获取父文件夹下的所有子文件夹\n",
    "subfolders = [f.path for f in os.scandir(parent_folder) if f.is_dir()]\n",
    "\n",
    "# 设置Microsoft Edge浏览器的下载路径\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 指定msedgedriver.exe的路径\n",
    "driver = 'D:\\小工具\\edgedriver\\msedgedriver.exe'\n",
    "# 启动Microsoft Edge浏览器\n",
    "# driver = webdriver.Edge()\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in subfolders:\n",
    "    # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "    item_id = os.path.basename(subfolder)\n",
    "\n",
    "    # 构建链接\n",
    "    formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在浏览器中打开链接\n",
    "    driver.get(url)\n",
    "\n",
    "    # 等待一段时间以确保下载完成\n",
    "    time.sleep(5)\n",
    "\n",
    "    # 移动下载好的表格到相应的文件夹\n",
    "    downloaded_file = max([os.path.join(download_folder, f) for f in os.listdir(download_folder)], key=os.path.getctime)\n",
    "    destination_path = os.path.join(subfolder, f\"{item_id}_{formatted_date}_data.xlsx\")\n",
    "    os.rename(downloaded_file, destination_path)\n",
    "\n",
    "# 关闭浏览器\n",
    "# driver.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e8bb9f05",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "max() arg is an empty sequence",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 51\u001b[0m\n\u001b[0;32m     48\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m5\u001b[39m)\n\u001b[0;32m     50\u001b[0m \u001b[38;5;66;03m# 移动下载好的表格到相应的文件夹\u001b[39;00m\n\u001b[1;32m---> 51\u001b[0m downloaded_file \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mmax\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdownload_folder\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mf\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mf\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlistdir\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdownload_folder\u001b[49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgetctime\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     52\u001b[0m destination_path \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(subfolder, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mitem_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m_\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mformatted_date\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m_data.xlsx\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     53\u001b[0m os\u001b[38;5;241m.\u001b[39mrename(downloaded_file, destination_path)\n",
      "\u001b[1;31mValueError\u001b[0m: max() arg is an empty sequence"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from selenium.webdriver.edge.service import Service as EdgeService\n",
    "from selenium.webdriver.edge.options import Options as EdgeOptions\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "import os\n",
    "from datetime import datetime, timedelta\n",
    "import time\n",
    "\n",
    "# 定义日期变量\n",
    "date_variable = datetime(2023, 12, 5)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "# 获取父文件夹下的所有子文件夹\n",
    "subfolders = [f.path for f in os.scandir(parent_folder) if f.is_dir()]\n",
    "\n",
    "# 设置Microsoft Edge浏览器的下载路径\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 指定msedgedriver.exe的路径\n",
    "webdriver_path = 'D:\\\\小工具\\\\edgedriver\\\\msedgedriver.exe'\n",
    "\n",
    "# 设置EdgeOptions对象\n",
    "edge_options = EdgeOptions()\n",
    "edge_options.binary_location = \"C:\\\\Program Files (x86)\\\\Microsoft\\\\Edge\\\\Application\\\\msedge.exe\"\n",
    "\n",
    "# 启动Microsoft Edge浏览器\n",
    "service = EdgeService(webdriver_path)\n",
    "driver = webdriver.Edge(service=service, options=edge_options)\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in subfolders:\n",
    "    # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "    item_id = os.path.basename(subfolder)\n",
    "\n",
    "    # 构建链接\n",
    "    formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在浏览器中打开链接\n",
    "    driver.get(url)\n",
    "\n",
    "    # 等待一段时间以确保下载完成\n",
    "    time.sleep(5)\n",
    "\n",
    "    # 移动下载好的表格到相应的文件夹\n",
    "    downloaded_file = max([os.path.join(download_folder, f) for f in os.listdir(download_folder)], key=os.path.getctime)\n",
    "    destination_path = os.path.join(subfolder, f\"{item_id}_{formatted_date}_data.xlsx\")\n",
    "    os.rename(downloaded_file, destination_path)\n",
    "\n",
    "# 关闭浏览器\n",
    "# driver.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e611a260",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.edge.service import Service as EdgeService\n",
    "from selenium.webdriver.edge.options import Options as EdgeOptions\n",
    "import os\n",
    "from datetime import datetime, timedelta\n",
    "import time\n",
    "\n",
    "# 定义日期变量\n",
    "date_variable = datetime(2023, 12, 5)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "# 获取父文件夹下的所有子文件夹\n",
    "subfolders = [f.path for f in os.scandir(parent_folder) if f.is_dir()]\n",
    "\n",
    "# 设置Microsoft Edge浏览器的下载路径\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 指定msedgedriver.exe的路径\n",
    "webdriver_path = 'D:\\\\小工具\\\\edgedriver\\\\msedgedriver.exe'\n",
    "\n",
    "# 设置EdgeOptions对象\n",
    "edge_options = EdgeOptions()\n",
    "edge_options.binary_location = \"C:\\\\Program Files (x86)\\\\Microsoft\\\\Edge\\\\Application\\\\msedge.exe\"\n",
    "# 设置为无头模式\n",
    "edge_options.add_argument(\"--headless\")\n",
    "\n",
    "# 启动Microsoft Edge浏览器\n",
    "service = EdgeService(webdriver_path)\n",
    "driver = webdriver.Edge(service=service, options=edge_options)\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in subfolders:\n",
    "    # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "    item_id = os.path.basename(subfolder)\n",
    "\n",
    "    # 构建链接\n",
    "    formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在浏览器中打开链接\n",
    "    driver.get(url)\n",
    "\n",
    "    # 等待一段时间以确保下载完成\n",
    "    time.sleep(5)\n",
    "\n",
    "    # 移动下载好的表格到相应的文件夹\n",
    "    downloaded_file = max([os.path.join(download_folder, f) for f in os.listdir(download_folder)], key=os.path.getctime)\n",
    "    destination_path = os.path.join(subfolder, f\"{item_id}_{formatted_date}_data.xlsx\")\n",
    "    os.rename(downloaded_file, destination_path)\n",
    "\n",
    "# 关闭浏览器\n",
    "# driver.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "70ad0a51",
   "metadata": {},
   "outputs": [
    {
     "ename": "TimeoutException",
     "evalue": "Message: \nStacktrace:\n\tGetHandleVerifier [0x00007FF646080342+58194]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF64600ECA2+216530]\n\t(No symbol) [0x00007FF645E29E1B]\n\t(No symbol) [0x00007FF645E79C6F]\n\t(No symbol) [0x00007FF645E79DA5]\n\t(No symbol) [0x00007FF645EBBB27]\n\t(No symbol) [0x00007FF645E9C11F]\n\t(No symbol) [0x00007FF645E6D3E1]\n\t(No symbol) [0x00007FF645EB8A7E]\n\t(No symbol) [0x00007FF645E9BD43]\n\t(No symbol) [0x00007FF645E6C162]\n\t(No symbol) [0x00007FF645E6B5BD]\n\t(No symbol) [0x00007FF645E6C994]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF64623722A+1051354]\n\t(No symbol) [0x00007FF645EEF596]\n\tMicrosoft::Applications::Events::EventProperty::clear [0x00007FF645F86205+34245]\n\tMicrosoft::Applications::Events::EventProperty::clear [0x00007FF645F7E6AD+2669]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF646235F23+1046483]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF646018988+256696]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF646014114+238148]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF646014242+238450]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF646008FE1+192785]\n\tBaseThreadInitThunk [0x00007FFA72377344+20]\n\tRtlUserThreadStart [0x00007FFA725C26B1+33]\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTimeoutException\u001b[0m                          Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[9], line 49\u001b[0m\n\u001b[0;32m     46\u001b[0m driver\u001b[38;5;241m.\u001b[39mget(url)\n\u001b[0;32m     48\u001b[0m \u001b[38;5;66;03m# 等待下载完成\u001b[39;00m\n\u001b[1;32m---> 49\u001b[0m \u001b[43mWebDriverWait\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdriver\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43muntil\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m     50\u001b[0m \u001b[43m    \u001b[49m\u001b[43mEC\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpresence_of_element_located\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[43mBy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mXPATH\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m//button[contains(text(), \u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m下载\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m)]\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     51\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     53\u001b[0m \u001b[38;5;66;03m# 移动下载好的表格到相应的文件夹\u001b[39;00m\n\u001b[0;32m     54\u001b[0m downloaded_file \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mmax\u001b[39m([os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(download_folder, f) \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m os\u001b[38;5;241m.\u001b[39mlistdir(download_folder)], key\u001b[38;5;241m=\u001b[39mos\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mgetctime)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\selenium\\webdriver\\support\\wait.py:105\u001b[0m, in \u001b[0;36mWebDriverWait.until\u001b[1;34m(self, method, message)\u001b[0m\n\u001b[0;32m    103\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m time\u001b[38;5;241m.\u001b[39mmonotonic() \u001b[38;5;241m>\u001b[39m end_time:\n\u001b[0;32m    104\u001b[0m         \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[1;32m--> 105\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m TimeoutException(message, screen, stacktrace)\n",
      "\u001b[1;31mTimeoutException\u001b[0m: Message: \nStacktrace:\n\tGetHandleVerifier [0x00007FF646080342+58194]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF64600ECA2+216530]\n\t(No symbol) [0x00007FF645E29E1B]\n\t(No symbol) [0x00007FF645E79C6F]\n\t(No symbol) [0x00007FF645E79DA5]\n\t(No symbol) [0x00007FF645EBBB27]\n\t(No symbol) [0x00007FF645E9C11F]\n\t(No symbol) [0x00007FF645E6D3E1]\n\t(No symbol) [0x00007FF645EB8A7E]\n\t(No symbol) [0x00007FF645E9BD43]\n\t(No symbol) [0x00007FF645E6C162]\n\t(No symbol) [0x00007FF645E6B5BD]\n\t(No symbol) [0x00007FF645E6C994]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF64623722A+1051354]\n\t(No symbol) [0x00007FF645EEF596]\n\tMicrosoft::Applications::Events::EventProperty::clear [0x00007FF645F86205+34245]\n\tMicrosoft::Applications::Events::EventProperty::clear [0x00007FF645F7E6AD+2669]\n\tMicrosoft::Applications::Events::EventProperty::to_string [0x00007FF646235F23+1046483]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF646018988+256696]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF646014114+238148]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF646014242+238450]\n\tMicrosoft::Applications::Events::ILogConfiguration::operator* [0x00007FF646008FE1+192785]\n\tBaseThreadInitThunk [0x00007FFA72377344+20]\n\tRtlUserThreadStart [0x00007FFA725C26B1+33]\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.edge.service import Service as EdgeService\n",
    "from selenium.webdriver.edge.options import Options as EdgeOptions\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "import os\n",
    "from datetime import datetime, timedelta\n",
    "import time\n",
    "\n",
    "# 定义日期变量\n",
    "date_variable = datetime(2023, 12, 5)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "# 获取父文件夹下的所有子文件夹\n",
    "subfolders = [f.path for f in os.scandir(parent_folder) if f.is_dir()]\n",
    "\n",
    "# 设置Microsoft Edge浏览器的下载路径\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 指定msedgedriver.exe的路径\n",
    "webdriver_path = 'D:\\\\小工具\\\\edgedriver\\\\msedgedriver.exe'\n",
    "\n",
    "# 设置EdgeOptions对象\n",
    "edge_options = EdgeOptions()\n",
    "edge_options.binary_location = \"C:\\\\Program Files (x86)\\\\Microsoft\\\\Edge\\\\Application\\\\msedge.exe\"\n",
    "# 设置为无头模式\n",
    "edge_options.add_argument(\"--headless\")\n",
    "\n",
    "# 启动Microsoft Edge浏览器\n",
    "service = EdgeService(webdriver_path)\n",
    "driver = webdriver.Edge(service=service, options=edge_options)\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in subfolders:\n",
    "    # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "    item_id = os.path.basename(subfolder)\n",
    "\n",
    "    # 构建链接\n",
    "    formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在浏览器中打开链接\n",
    "    driver.get(url)\n",
    "\n",
    "    # 等待下载完成\n",
    "    WebDriverWait(driver, 10).until(\n",
    "        EC.presence_of_element_located((By.XPATH, '//button[contains(text(), \"下载\")]'))\n",
    "    )\n",
    "\n",
    "    # 移动下载好的表格到相应的文件夹\n",
    "    downloaded_file = max([os.path.join(download_folder, f) for f in os.listdir(download_folder)], key=os.path.getctime)\n",
    "    destination_path = os.path.join(subfolder, f\"{item_id}_{formatted_date}_data.xlsx\")\n",
    "    os.rename(downloaded_file, destination_path)\n",
    "\n",
    "# 关闭浏览器\n",
    "# driver.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5635c5f1",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 56\u001b[0m\n\u001b[0;32m     54\u001b[0m \u001b[38;5;66;03m# 显式等待文件下载完成\u001b[39;00m\n\u001b[0;32m     55\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m---> 56\u001b[0m     \u001b[43mWebDriverWait\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdriver\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m60\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43muntil\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m     57\u001b[0m \u001b[43m        \u001b[49m\u001b[43mEC\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpresence_of_element_located\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[43mBy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mXPATH\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m//button[contains(text(), \u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m下载\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m)]\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     58\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     59\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m TimeoutException:\n\u001b[0;32m     60\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m文件下载超时\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python311\\site-packages\\selenium\\webdriver\\support\\wait.py:102\u001b[0m, in \u001b[0;36mWebDriverWait.until\u001b[1;34m(self, method, message)\u001b[0m\n\u001b[0;32m    100\u001b[0m     screen \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(exc, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mscreen\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m    101\u001b[0m     stacktrace \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(exc, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstacktrace\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m--> 102\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_poll)\n\u001b[0;32m    103\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m time\u001b[38;5;241m.\u001b[39mmonotonic() \u001b[38;5;241m>\u001b[39m end_time:\n\u001b[0;32m    104\u001b[0m     \u001b[38;5;28;01mbreak\u001b[39;00m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.edge.service import Service as EdgeService\n",
    "from selenium.webdriver.edge.options import Options as EdgeOptions\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.common.exceptions import TimeoutException\n",
    "import os\n",
    "from datetime import datetime, timedelta\n",
    "import time\n",
    "\n",
    "# 定义日期变量\n",
    "date_variable = datetime(2023, 12, 5)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "# 获取父文件夹下的所有子文件夹\n",
    "subfolders = [f.path for f in os.scandir(parent_folder) if f.is_dir()]\n",
    "\n",
    "# 设置Microsoft Edge浏览器的下载路径\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 指定msedgedriver.exe的路径\n",
    "webdriver_path = 'D:\\\\小工具\\\\edgedriver\\\\msedgedriver.exe'\n",
    "\n",
    "# 设置EdgeOptions对象\n",
    "edge_options = EdgeOptions()\n",
    "edge_options.binary_location = \"C:\\\\Program Files (x86)\\\\Microsoft\\\\Edge\\\\Application\\\\msedge.exe\"\n",
    "\n",
    "# 设置下载文件的路径\n",
    "prefs = {\"download.default_directory\": download_folder}\n",
    "edge_options.add_experimental_option(\"prefs\", prefs)\n",
    "\n",
    "# 设置为无头模式\n",
    "edge_options.add_argument(\"--headless\")\n",
    "\n",
    "# 启动Microsoft Edge浏览器\n",
    "service = EdgeService(webdriver_path)\n",
    "driver = webdriver.Edge(service=service, options=edge_options)\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in subfolders:\n",
    "    # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "    item_id = os.path.basename(subfolder)\n",
    "\n",
    "    # 构建链接\n",
    "    formatted_date = date_variable.strftime(\"%Y-%m-%d\")\n",
    "    url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id}&device=2&order=desc&orderBy=uv\"\n",
    "\n",
    "    # 在浏览器中打开链接\n",
    "    driver.get(url)\n",
    "\n",
    "    # 显式等待文件下载完成\n",
    "    try:\n",
    "        WebDriverWait(driver, 60).until(\n",
    "            EC.presence_of_element_located((By.XPATH, '//button[contains(text(), \"下载\")]'))\n",
    "        )\n",
    "    except TimeoutException:\n",
    "        print(\"文件下载超时\")\n",
    "\n",
    "    # 等待一段时间以确保下载完成\n",
    "    time.sleep(5)\n",
    "\n",
    "    # 移动下载好的表格到相应的文件夹\n",
    "    downloaded_files = [f for f in os.listdir(download_folder) if f.endswith('.xlsx')]\n",
    "    if downloaded_files:\n",
    "        downloaded_file = max([os.path.join(download_folder, f) for f in downloaded_files], key=os.path.getctime)\n",
    "        destination_path = os.path.join(subfolder, f\"{item_id}_{formatted_date}_data.xlsx\")\n",
    "        os.rename(downloaded_file, destination_path)\n",
    "    else:\n",
    "        print(\"未找到下载的文件\")\n",
    "\n",
    "# 关闭浏览器\n",
    "# driver.quit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "abe1072a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 35\u001b[0m\n\u001b[0;32m     32\u001b[0m webbrowser\u001b[38;5;241m.\u001b[39mopen(url)\n\u001b[0;32m     34\u001b[0m \u001b[38;5;66;03m# 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\u001b[39;00m\n\u001b[1;32m---> 35\u001b[0m \u001b[43mtime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msleep\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     37\u001b[0m \u001b[38;5;66;03m# 获取下载好的表格文件\u001b[39;00m\n\u001b[0;32m     38\u001b[0m downloaded_files \u001b[38;5;241m=\u001b[39m [f\u001b[38;5;241m.\u001b[39mpath \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m os\u001b[38;5;241m.\u001b[39mscandir(download_folder) \u001b[38;5;28;01mif\u001b[39;00m f\u001b[38;5;241m.\u001b[39mis_file()]\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 18)\n",
    "end_date = datetime(2023, 12, 20)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in os.scandir(parent_folder):\n",
    "    if subfolder.is_dir():\n",
    "        # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "        item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "        # 循环遍历日期范围\n",
    "        current_date = start_date\n",
    "        while current_date <= end_date:\n",
    "            # 格式化日期\n",
    "            formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "            # 构建链接\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "\n",
    "            # 在默认浏览器中打开链接\n",
    "            webbrowser.open(url)\n",
    "\n",
    "            # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "            time.sleep(10)\n",
    "\n",
    "            # 获取下载好的表格文件\n",
    "            downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "            if downloaded_files:\n",
    "                # 如果有文件，取最新的一个\n",
    "                downloaded_file = max(downloaded_files, key=os.path.getctime)\n",
    "\n",
    "                # 复制下载好的表格到相应的子文件夹\n",
    "                destination_path = os.path.join(subfolder.path, f\"{os.path.basename(downloaded_file)}\")\n",
    "                shutil.copy(downloaded_file, destination_path)\n",
    "                print(f\"Copied file to: {destination_path}\")\n",
    "\n",
    "                # 删除下载目录中的源文件\n",
    "                os.remove(downloaded_file)\n",
    "                print(f\"Removed source file: {downloaded_file}\")\n",
    "            else:\n",
    "                print(f\"No files downloaded for item_id {item_id_variable} on {formatted_date}\")\n",
    "\n",
    "            # 增加一天\n",
    "            current_date += timedelta(days=1)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
